package com.xiaoge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.annotation.ControllerEndpoint;
import com.xiaoge.entity.User;
import com.xiaoge.entity.WebsiteLink;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.response.Result;
import com.xiaoge.service.WebsiteLinkService;
import com.xiaoge.vo.WebsiteLinkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * <p>
 * 站点友情链接 前端控制器
 * </p>
 *
 * @author XiaoGe
 * @since 2022-10-28
 */
@Api(value = "站点友情链接")
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/WebsiteLink")
public class WebsiteLinkController {

    @Autowired
    private WebsiteLinkService websiteLinkService;


    /**
     * 模糊+条件获取友情链接信息
     * @param current
     * @param size
     * @param websiteLinkVO
     * @return
     */
    @ApiOperation("模糊+条件获取友情链接信息")
    @PostMapping("/getWebsiteLinkList")
    public Result getWebsiteLinkList(@RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
                                     @RequestParam(value = "size",required = true,defaultValue = "7") Integer size,
                                     @RequestBody WebsiteLinkVO websiteLinkVO){

        Page<WebsiteLink> page = new Page<>(current, size);

        Page<WebsiteLink> websiteLinkPage = websiteLinkService.getWebsiteLinkList(page,websiteLinkVO);

        return Result.ok().data("websiteLinkPage",websiteLinkPage);
    }

    /**
     * 删除链接信息
     *
     * @param id 用户ID
     * @return
     */
    @ApiOperation(value = "删除链接信息", notes = "删除链接信息，根据用户ID")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {

        websiteLinkService.deleteById(id);

        return Result.ok();
    }

    /**
     * 编辑链接信息
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑链接信息", notes = "获取链接信息的详情，编辑链接信息信息")
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable Long id) {

        WebsiteLink websiteLink = websiteLinkService.selectById(id);

        return Result.ok().data("websiteLink",websiteLink);
    }

    /**
     * 更新用户
     * @param websiteLink
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新链接信息失败", operation = "更新链接信息")
    @ApiOperation(value = "更新链接信息", notes = "更新链接信息信息")
    @PutMapping("/update")
    public Result update(@RequestBody @Validated WebsiteLink websiteLink) {

       websiteLinkService.updateWebsiteLink(websiteLink);

        return Result.ok();
    }


    /**
     * 添加链接信息信息
     * @param websiteLink
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加链接信息失败", operation = "添加链接信息")
    @ApiOperation(value = "添加链接信息", notes = "添加链接信息")
    @PostMapping("/add")
    public Result add(@RequestBody WebsiteLink websiteLink) {

        websiteLinkService.addWebsiteLink(websiteLink);

        return Result.ok();
    }


}
